package com.cloud.service;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cloud.core.BaseService;
import com.cloud.dao.PictureBean;
import com.cloud.dao.impl.PictureDao;
import com.cloud.util.CommUtil;
import com.cloud.util.Picture;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-12-4
 * @version 1.0 
 */
@Service("FileService")
public class FileService extends BaseService{
	private static final Logger logger = LoggerFactory.getLogger(FileService.class);
	
	@Value("${cloud.picture.root}")
	public String pictureroot;
	@Value("${cloud.picture.visit}")
	public String picturevisit;
	
	@Autowired
	private PictureDao PictureDao;
	
	/**
	 * 批量上传
	 * @param files
	 * @param picture
	 * @return
	 */
	public String[] uploadPicture(MultipartFile[] files, Picture picture){
		int index = 0;
		String[] fileIds = new String[files.length];
		for(MultipartFile file : files){
			fileIds[index++] = uploadPicture(file, picture);
		}
		return fileIds;
	}
	
	/**
	 * 文件上传
	 * @param file
	 * @param picture
	 * @return
	 */
	public String uploadPicture(MultipartFile file, Picture picture){
		if(file.isEmpty())
			return "";
		PictureBean bean = new PictureBean();
		bean.setPicId(CommUtil.getUUID());
		String fileName = file.getOriginalFilename();
		bean.setPicName(fileName);
		String format = fileName.substring(fileName.lastIndexOf("."), fileName.length());
		bean.setPicType(String.valueOf(picture.key));
		bean.setPicSuffix(format.toLowerCase());
		bean.setPicSize(String.valueOf(file.getSize()));
		bean.setPicPath(genelFilePath(bean));
		logger.debug(String.format("开始上传图片[%s]的信息.", bean.getPicPath()));
		try {
			file.transferTo(new File(bean.getPicPath()));
			//FileUtils.copyInputStreamToFile(file.getInputStream(), new File(bean.getPicPath()));
			addPicture(bean);
		} catch (IllegalStateException e) {
			logger.error("上传图片失败",e);
			return "";
		} catch (IOException e) {
			logger.error("上传图片失败",e);
			return "";
		}
		return bean.getPicId();
	}
	
	/**
	 * 覆盖文件
	 * @param file
	 * @param newFile
	 */
	public void uploadPicture(MultipartFile file, String newFile){
		if(!file.isEmpty()){
			try {
				file.transferTo(new File(newFile));
			} catch (IllegalStateException e) {
				logger.error("上传图片失败",e);
			} catch (IOException e) {
				logger.error("上传图片失败",e);
			}
		}
	}
	
	/**
	 * 添加图片
	 * @param bean
	 */
	private void addPicture(PictureBean bean){
		logger.debug(String.format("开始添加图片[%s]的信息.", bean.getPicId()));
		PictureDao.addPicture(bean);
	}
	
	private String genelFilePath(PictureBean bean){
		String sfolder = CommUtil.getPicturePath(Integer.valueOf(bean.getPicType()));
		String pfolder = getPictureroot();
		if(sfolder != null && sfolder.trim().length() != 0){
			pfolder = pfolder.concat(sfolder).concat(File.separator);
		}
		File folder = new File(pfolder);
		if (!folder.exists())
			folder.mkdirs();
		String path = pfolder.concat(bean.getPicId()).concat(bean.getPicSuffix());
		return path;
	}
	
	/**
	 * 查询图片
	 * @param bean
	 * @return
	 */
	public List<PictureBean> getPictureList(PictureBean bean){
		logger.debug(String.format("开始查询图片[%s]的信息.", bean.getPicId()));
		return PictureDao.getPictureList(bean);
	}
	
	public void updatePicture(PictureBean bean){
		PictureDao.updatePicture(bean);
	}
	
	public void deletePicture(PictureBean bean){
		File file = new File(bean.getPicPath());
		if(file.isFile() && file.exists())
			file.delete();
		PictureDao.deletePicture(bean);
	}

	public String getPictureroot() {
		return pictureroot;
	}

	public void setPictureroot(String pictureroot) {
		this.pictureroot = pictureroot;
	}

	public String getPicturevisit() {
		return picturevisit;
	}

	public void setPicturevisit(String picturevisit) {
		this.picturevisit = picturevisit;
	}
	
	public String getVisitUrl(String path){
		if(CommUtil.isEmpty(path))
			return "";
		return getPicturevisit().concat(path.substring(getPictureroot().length()));
	}
}
